package com.linhh.mall.center.common.util;

import com.linhh.mall.center.common.exception.DefinitionException;

/**
 * @Author linhh
 * @Date 2022 10 16 10 49
 * 响应状态
 **/
public class HttpResult<T> {

    private Integer code; // 状态码

    private String message; // 提示信息

    private T data; // 数据

    public HttpResult(){
    }

    /**
     * 响应编码和信息
     * @param code 响应编码
     * @param message 响应信息
     */
    public HttpResult(Integer code,String message){
        this.code = code;
        this.message = message;
    }

    /**
     * 响应编码，信息，数据
     * @param code 响应编码
     * @param message 响应信息
     * @param data 响应数据
     */
    public HttpResult(Integer code,String message,T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 自定义枚举类 包含code和message
     * @param resultEnum
     */
    public HttpResult(ResultEnum resultEnum){
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    /**
     *  自定义枚举类 包含code和message,data
     * @param resultEnum
     * @param data
     */
    public HttpResult(ResultEnum resultEnum,T data){
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
        this.data = data;
    }
    /**
     * 自定义异常返回结果
     * @param definitionException 自定义异常处理
     * @return 返回自定义异常
     */
    public static HttpResult<Object> defineError(DefinitionException definitionException){
        return new HttpResult<>(definitionException.getErrorCode(),definitionException.getErrorMessage());
    }
    /**
     * 其他异常处理方法返回的结果
     * @param resultEnum 自定义枚举类 包含，code和message
     * @return 返回其他异常
     */
    public static HttpResult<Object> otherError(ResultEnum resultEnum){
        return new HttpResult<>(resultEnum);
    }

    /**
     * 执行成功是返回SUCCESS"成功"和数据data
     * @param data
     * @return
     */
    public static HttpResult<Object> success(Object data){
        return new HttpResult<>(ResultEnum.SUCCESS,data);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
